package com.just.login.realm;

import com.just.login.Model.User;
import com.just.login.dao.Userdao;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

public class CustomerMd5Realm extends AuthorizingRealm {

    Userdao dao = new Userdao();
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String principal = (String) authenticationToken.getPrincipal();
        User user = dao.getUserByAccount(principal);
        if (user!=null && user.getUsername().equals(principal)) {
            System.out.println(user);
            SimpleAuthenticationInfo simpleAuthenticationInfo =
                    new SimpleAuthenticationInfo(
                            user.getUsername(),
                            user.getPassword(),
                            ByteSource.Util.bytes(user.getSalt()),
                            this.getName());
            return simpleAuthenticationInfo;
        }
        return null;
    }
}
